package com.ascentive.finallyfast.deepclean;

import java.util.HashSet;
import java.util.Set;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import com.ascentive.finallyfast.FinallyFastApplication;

public class SmsDraftsLog extends SmsLog{
	
	int noOfMessagesDeleted = 0;
	
	public SmsDraftsLog(FinallyFastApplication app, String name, boolean value, int img) {
		super(app, name, value, img);
	}

	@Override
	void doClean() {
		
		int smsDeleteStatus = 0;
		 
		 // URI for sms
        Uri uriSmsInbox = Uri.parse("content://sms/draft");
        	
        ContentResolver cr = mApp.getContentResolver();
        Cursor smsDraftCursor = cr.query(uriSmsInbox, new String[] { "thread_id" },
       		 "date <= (" + Utility.getDateBeforeNDays(mApp.getTargetDateDuration() ) +")", null, null);
        // if Draft is Empty
        if(smsDraftCursor.getCount() == 0) 
        	return;
        
        try {
            Set<String> threads = new HashSet<String>();

            while (smsDraftCursor.moveToNext()) {
                threads.add(smsDraftCursor.getString(0));
            }

            for (String thread : threads) {
                // Delete the SMS threads
                String uri = "content://sms/conversations/" + thread;
                smsDeleteStatus = cr.delete(Uri.parse(uri), null, null);
                noOfMessagesDeleted += smsDeleteStatus;
            }
        } catch (Exception e) {
            Log.e("LOG_TAG", "exception in deleteSms:", e);
        }
		return;
	}
	@Override
	int numberOfFileCleaned() {
		return noOfMessagesDeleted;
	}
}
